#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<sys/time.h>

#define MYPOW 1
int mypow(int x, int y)
{
	int pow = 1;
	int i = 0;

	for(i = 0; i < y; i++){
		pow = pow * x;
	}
	
	return pow;
}

int getRandomNumber(int from, int to)
{
	int num = 0;
	int randnum = 0;

	srand((unsigned int)time((time_t *)NULL));
	randnum = rand();
	printf("from=[%d], to=[%d], rand=[%d]\n", from, to, randnum);
	num = (randnum % (to - from)) + from;
	
	return num;
}

int getDelayTime(int acsConnectFails)
{
	int delayTime = 0;
	int minWaitInterval = 5;
	int intervalMultiplier = 2000;
	int min=0, max=0;

	if (acsConnectFails > 10)
	{
		acsConnectFails = 10;
	}

	printf("acsConnectFails=[%d]\n", acsConnectFails);
#ifndef USE_MYPOW
	min = minWaitInterval * (pow(((float)intervalMultiplier/1000.0),(acsConnectFails-1)));
	max = minWaitInterval * (pow(((float)intervalMultiplier/1000.0),acsConnectFails));
#else
	min = minWaitInterval * (mypow(((float)intervalMultiplier/1000.0),(acsConnectFails-1)));
	max = minWaitInterval * (mypow(((float)intervalMultiplier/1000.0),acsConnectFails));
#endif
	delayTime = getRandomNumber(min, max);

	return delayTime;
}

void main(int argc, char **argv)
{
	int i = 0;
	int uplimit = 1;

	if(argc > 1)
		uplimit = atoi(argv[1]);

	for(i = 1; i < uplimit; i++)
		printf("dbg-----> i=[%d], time=[%d]\n", i, getDelayTime(i));
}
